<?php

class User extends CI_Model {


	function __construct() {
		// Call the Model constructor
		parent::__construct();
	}

	function getUsername($userId) {

		$data = array('userId' => $userId);

		$query = $this -> db -> get_where('user', $data);

		if ($query -> num_rows() > 0) {
			foreach ($query->result() as $q) {
				return $q -> username;
			}
		}

		return '';
	}

//	function getUsernameAndTwitterInfo($userId) {
//
//		// checking for twitter
//		$data = array('userId' => $userId);
//		$query = $this -> db -> get_where('twitterInfo', $data);
//
//		$returnData = array('username' => '', 'isTwitter' => '');
//
//		if ($query -> num_rows() > 0) {
//			foreach ($query->result() as $q) {
//				$returnData['isTwitter'] = '1';
//			}
//		} else {
//			$returnData['isTwitter'] = '0';
//		}
//
//		// regular username
//		$query = $this -> db -> get_where('user', $data);
//
//		if ($query -> num_rows() > 0) {
//			foreach ($query->result() as $q) {
//				$returnData['username'] = $q -> username;
//			}
//		}
//
//		return $returnData;
//	}

	function getUserId($username) {

		$data = array('username' => $username);
		$query = $this -> db -> get_where('user', $data);

		if ($query -> num_rows() > 0) {
			foreach ($query->result() as $q) {
				return $q -> userId;
			}
		} else {
			return 0;
		}

	}
	
	/**
	 * 
	 * @return int id
	 *
	 */

	function insertUser($twitterId) {
		$query = $this->db->get_where('user',array('twitterId'=>$twitterId));	
		$count = $query->num_rows();
		if($count == 0) {
			$this->db->insert('user',array('twitterId'=>$twitterId));
                        return $this->db->insert_id();
		}	
		$row = $query->row();
		return $row->userId;
	}
        
        function getTwitterId($userDbId) {
            $query = $this->db->get_where('user',array('userId'=>$userDbId));
            foreach($query->result() as $q) {
                return $q->twitterId;
            }
            $row = $query->row();
            return $row->twitterId;
        }
            

	function addUser($username) {

		$data = array('username' => $username);
		$query = $this -> db -> insert('user', $data);
		$userId = $this -> db -> insert_id();

        $consumer_key = "removed";
        $consumer_secret = "removed";
        #$req_token_url = "https://api.twitter.com/oauth/request_token";
        $auth_url = "https://api.twitter.com/oauth2/token";
        #$auth_token_url = "https://api.twitter.com/oauth/access_token";
        $secret_string = base64_encode($consumer_key . ':' . $consumer_secret);

        $response = http_post_data($auth_url, 'grant_type=client_credentials',
            array("Authorization" => "Basic $secret_string",
                  "Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8",
            ));
            
       # https://dev.twitter.com/docs/auth/application-only-auth 

        


		$url = "https://api.twitter.com/1/users/show.json?screen_name=" . $username;

		$info = json_decode(file_get_contents($url), TRUE);

		if (empty($info['profile_banner_url'])) {
			$bg = "https://abs.twimg.com/a/1366303520/t1/img/grey_header_web.png";
		} else {
			$bg = $info['profile_banner_url'] . "/web";
		}

		$data = array('userId' => $userId, 'profileImageURL' => $info['profile_image_url'], 'profileBannerURL' => $bg, 'followers' => $info['followers_count'], 'isVerified' => $info['verified']);

		$this -> db -> insert('twitterInfo', $data);

		return $userId;

	}

}
?>
